<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        取消
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" style="width: 50%;margin: 0 auto;">
        <a-form-item label="支出类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
             <a-select placeholder="请输入支出类别" :disabled="disabled" v-decorator.trim="[ 'type', validatorRules.type]"  :dropdownMatchSelectWidth="false"
           showSearch optionFilterProp="children">
               <a-select-option v-for="(item,index) in operatorList" :key="index" :value="item.name" >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item  label="支出账户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入支出账户" :disabled="disabled" v-decorator.trim="[ 'account', validatorRules.account]"/>
        </a-form-item>
        <a-form-item  label="接收用户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入接收用户" :disabled="disabled" v-decorator.trim="[ 'receivee', validatorRules.receivee]"/>
        </a-form-item>
        <!-- <a-form-item  label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入联系电话" :maxLength="11" v-decorator.trim="[ 'tel', validatorRules.tel]"/>
        </a-form-item> -->
        <!-- <a-form-item label="收益总和" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input placeholder="请输入收益总和"  v-decorator.trim="[ 'profit', validatorRules.profit]"/>
        </a-form-item>
        -->
        <a-form-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请输入业务员" :disabled="disabled" v-decorator.trim="[ 'ope', validatorRules.ope]"  :dropdownMatchSelectWidth="false"
           showSearch optionFilterProp="children" >
           <a-select-option v-for="(item,index) in opeList" :key="index" :value="item.name" >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item> 
        <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol" >
         <a-input placeholder="请输入金额" :disabled="disabled" v-decorator.trim="[ 'amount', validatorRules.amount]" />
       </a-form-item>
       <a-form-item label="用途" :labelCol="labelCol" :wrapperCol="wrapperCol" >
         <!-- <a-input placeholder="请输入用途" v-decorator.trim="[ 'purpose', validatorRules.purpose]" /> -->
         <a-textarea placeholder="请输入用途" :disabled="disabled" :rows="5" v-decorator.trim="[ 'purpose', validatorRules.purpose]" />
       </a-form-item>
       <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" >
         <!-- <a-input placeholder="请输入备注" v-decorator.trim="[ 'remark', validatorRules.remark]" /> -->
         <a-textarea placeholder="请输入备注" :disabled="disabled" :rows="5" v-decorator.trim="['remark', validatorRules.remark]" />
       </a-form-item>
       <a-form-item label="生成时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请输入生成时间" :disabled="disabled" v-model="model.updateTime" v-decorator.trim="[ 'updateTime', validatorRules.updateTime]" ></j-date>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="附件">
            <j-image-upload v-model="fileList"  :disabled="disabled" v-decorator.trim="[ 'fileName', validatorRules.fileName]" bizPath="business" text="上传" isMultiple></j-image-upload>
        </a-form-item>
        <div>
        </div>
        
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import JDate from '@/components/jeecg/JDate'
  import { FormTypes } from '@/utils/JEditableTableUtil'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { getConfigSourceList} from '@/api/api'
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
import { addExpenditure, editExpenditure,getAllListIncome, getConfigOperatorList, getExpenditureInfoById } from '../../../api/api'
  export default {
    name: "EngineerModal",
    mixins: [JEditableTableMixin],
    components: {
      JImageUpload,
      JSelectPosition,
      JImageUpload,
      JDate
    },
    data () {
      return {
        sourceList: [],
        opeList:[],
        title:"操作",
        visible: false,
        operatorList:[],
        refKeys: ['materialDataTable', ],
        tableKeys:['materialDataTable', ],
        activeKey: 'materialDataTable',
        modalWidth:800,
        drawerWidth:700,
        // credit_options : ["风险", "正常", "优秀", "极好","黑名单"],
        fileList:[],
        id:"", //保存工程id
        isReadOnly: false,
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          updateTime:{
            rules: [
              {
              required: true, message: '请输入生成时间'
            }
            ]
          },
          type:{
            rules: [{
              required: true, message: '请输入支出类别'
            }]
          },
          account:{
            rules: [
              {
              required: true, message: '请输入支出账户'
            }
            ]
          },
          ope:{
            rules: [
              {
              required: true, message: '请输入业务员'
            }
            ]
          },
          amount:{
            rules: [{
              required: true, message: '请输入金额', type: 'number', trigger: 'blur',
               transform (value) {
                  var moneyg = /^\d+$|^\d*\.\d+$/g
                  if (!moneyg.test(value) ) {
                    return false
                  }else{
                    return Number(value)
                  }
                }
            }]
          },
          receivee:{
            rules: [
              {
              required: true, message: '请输入接收用户'
            }
            ]
          },
          remark:{
            rules: [
              {
              required: false, message: '请输入备注'
            }
            ]
          },
          purpose:{
            rules: [
              {
              required: false, message: '请输入用途'
            }
            ]
          },
          fileName:{
            rules: [
              {
              required: false, message: '请上传附件'
            }
            ]
          }
        },
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        wrapperCol1: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        disabled:false,
        uploadLoading:false,
        // confirmLoading: false,
        headers:{},
        // form:this.$form.createForm(this)
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },
    mounted () {
      this.getOpe()
      this.getIncomeConfig()
    },
    methods: {
          // 获取收入/支出配置
    getIncomeConfig(){
      getAllListIncome({
          type:'支出'
      }).then(res => {
        this.operatorList.splice(0,this.operatorList.length,...res.data.operatorList)
      })
    },
      show(record){
        this.visible = true;
        let params = {
          id : record.id
        }
        this.disabled = true
        let that = this
        // 通过获取单个数据的接口(/custom/selectOne)1
        getExpenditureInfoById(params).then(res => {
              console.log(res,'res');
              // console.log(res.data,'getCustomOne');
              that.model = Object.assign({}, res.data);
              that.form.setFieldsValue(pick(that.model,'type','account','amount','ope','receivee','remark','purpose','fileName','updateTime'))
            })
      },
      getOpe(){
        let that = this;
      getConfigOperatorList({}).then((res)=>{
        if(res) {
          res.data.operatorList.forEach(item => {
            item.value = item.name;
            item.title = item.name;
          })
          that.opeList = res.data.operatorList;
        }
      });
      },
      getSourceList(params) {
      let that = this;
      getConfigSourceList(params).then((res)=>{
        if(res) {
          that.sourceList = res.data.operatorList;
        }
      });
    },
      add () {
        // this.materialTable.dataSource.splice(0,this.materialTable.dataSource.length)
        this.edit({});
      },
      reset(){
          Object.keys(this.model).forEach(key => {
            this.model[key] = '';
          })
      },
      edit (record) {
        this.disabled = false
        let that = this;
        that.form.resetFields();
        that.visible = true;
        if(record.id == null){
          // Object.
          this.reset()
          console.log(this.model,'model');
          return
        }
        let params = {
          id : record.id
        }
        that.id = record.id;
    // 通过获取单个数据的接口(/custom/selectOne)1
    getExpenditureInfoById(params).then(res => {
          console.log(res,'res');
          // console.log(res.data,'getCustomOne');
          that.model = Object.assign({}, res.data);
          that.form.setFieldsValue(pick(that.model,'type','account','amount','ope','receivee','remark','purpose','fileName'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      loadData(){
        this.$emit('save');
      },
      classifyIntoFormData(allValues) {
        let billMain = Object.assign(this.model, allValues.formValue)
        let detailArr = allValues.tablesValue[0].values
        console.log(billMain,'billMain');
        console.log(detailArr,'detailArr');
        if(this.model.id){
          billMain.id = this.model.id
        }
        return {
          info: JSON.stringify(billMain),
          rows: JSON.stringify(detailArr),
        }
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            that.confirmLoading = true;
            let obj;
            if(!this.model.id){
              formData.id = this.id;
              obj=addExpenditure(formData);
            }else{
              obj=editExpenditure(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.loadData()
              that.confirmLoading = false;
              that.close();
            })
          }
          })
        // return this.request(formData)
      
        // this.form.validateFields((err, values) => {
        //   if (!err) {
        //     that.confirmLoading = true;
        //     let formData = Object.assign(this.model,values);
        //     let obj;
        //     if(!this.model.id){
        //       formData.id = this.id;
        //       obj=addCustom(formData);
        //     }else{
        //       obj=editCustom(formData);
        //     }
        //     obj.then((res)=>{
        //       if(res.code === 200){
        //         that.$emit('ok');
        //       }else{
        //         that.$message.warning(res.data.message);
        //       }
        //     }).finally(() => {
        //       that.confirmLoading = false;
        //       that.close();
        //     })
        //   }
        // })
      },
      handleCancel() {
        // this.materialTable.dataSource.splice(0,this.materialTable.dataSource.length)
        this.close()
      },

      validateName(rule, value, callback){
        let params = {
          name: value,
          id: this.model.id?this.model.id:0
        };
        checkEngineer(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data.status){
              callback();
            } else {
              callback("名称已经存在");
            }
          } else {
            callback(res.data);
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>